import { PageQueryOptions } from './api/temu';
import { WorkFlowApi } from './lib/comfyUI';
import { GetProductTitleOptions } from './lib/postme';

// background 那边不能有两个监听端
export enum MessageType {
  // content-script 发送到 background 用来获取产品的详细信息,有返回
  getGiga2bProductInfo = 'get-giga2b-product-info',

  // content-script 发送到 background 用来获取产品的库存和价格
  getGigaProductPrice = 'get-giga-product-price',

  // content-script 发送到 background 用来创建和获取产品的 title,有返回
  generatorProductTitle = 'generator-product-title',

  // content-script 发送到 background 用来获取 access token,有返回
  getGigaAccessToken = 'get-giga-access-token',

  // content-script 发送到 background 用来获取 giga 资源包,有返回
  getGigaResource = 'get-giga-resource',

  // content-script 发送到 background 用来破解验证码
  solveGigaCaptcha = 'solve-giga-captcha',

  // content-script 发送到 background 用来设置代理
  setProxy = 'set-proxy',

  // content-script 发送到 background 用来获取代理
  getProxy = 'get-proxy',

  // background 发送到 content-script 用来打开采集详情
  gigaResourceInfo = 'giga-resource-info',

  // popup 发送到 content-script 用来打开使用说明
  openOperationInstruction = 'open-operation-instruction',

  // content-script 发送到 background 用来获取 temu 的用户信息
  getTemuUserInfo = 'get-temu-user-info',

  // content-script 发送到 background 用来获取 temu 的产品信息
  getTemuPageQuery = 'get-temu-page-query',

  // content-script 发送到 background 用来解决跨域问题
  corssRequest = 'corss-request',
}

export interface GetGiga2bProductInfoMessage {
  type: MessageType.getGiga2bProductInfo;
  data: {
    productId: string;
    sku: string;
    accessToken: string;
    tokenType: string;
  };
}

export interface GetGigaProductPriceMessage {
  type: MessageType.getGigaProductPrice;
  data: {
    gigaProductId: string;
  };
}

export interface GeneratorProductTitleMessage {
  type: MessageType.generatorProductTitle;
  data: {
    getProductTitleOptions: GetProductTitleOptions;
  };
}

export interface GetGigaAccessTokenMessage {
  type: MessageType.getGigaAccessToken;
  data: {
    clientId: string;
    clientSecret: string;
  };
}

export interface GetGigaResourceMessage {
  type: MessageType.getGigaResource;
  data: {
    gigaProductId: string;
  };
}

export interface SolveGigaCaptchaMessage {
  type: MessageType.solveGigaCaptcha;
  data: {
    gigaProductId: string;
  };
}

export interface SetProxyMessage {
  type: MessageType.setProxy;
  data: any;
}

export interface GetProxyMessage {
  type: MessageType.getProxy;
}

export interface GigaResourceInfoMessage {
  type: MessageType.gigaResourceInfo;
}

export interface OpenOperationInstructionMessage {
  type: MessageType.openOperationInstruction;
}

export interface GetTemuUserInfoMessage {
  type: MessageType.getTemuUserInfo;
}

export interface GetTemuPageQueryMessage {
  type: MessageType.getTemuPageQuery;
  data: {
    options: PageQueryOptions;
    mallId: number;
  };
}

export interface CorssRequestMessage {
  type: MessageType.corssRequest;
  data:
    | {
        kind: 'comfyUiGetImageGenerationLocation';
        config: {
          url: string;
        };
      }
    | {
        kind: 'comfyUiUploadImage';
        config: {
          url: string;
          dataUrl: string;
          name: string;
        };
      }
    | {
        kind: 'comfyUiCreateImage';
        config: {
          url: string;
          prompt: WorkFlowApi;
        };
      }
    | {
        kind: 'comfyUiDownloadImage';
        config: {
          url: string;
        };
      };
}

export enum PostMessageType {
  // diaoxiaomi content-script 发送到 dianxiaomi-meituxiuxiu-html 用来换背景
  'changeBackground' = '换背景',

  // diaoxiaomi content-script 发送到 dianxiaomi-meituxiuxiu-html 用来提示设置更新
  'processSettingUpdate' = '更新加工配置',

  // dianxiaomi-meituxiuxiu-html 发送到 diaoxiaomi content-script 用来表示获取到替换背景后的图片
  'getChangedBackgroundResult' = '获取换背景结果',

  // temu 页面中 content-script 发送到 iframe 来打开弹框
  openTemuDialog = 'open-temu-dialog',

  // temu 的 iframe 发送到 content-script 用来关闭弹框
  closeTemuDialog = 'close-temu-dialog',

  // temu 的 iframe 发送到 content-script 用来保存配置
  saveTemuSettings = 'save-temu-settings',

  // temu 的 iframe 发送到 content-script 用来导入配置
  importTemuSettings = 'import-temu-settings',

  // temu 页面中 content-script 发送到 iframe 来发送配置
  sendTemuSettings = 'send-temu-settings',

  // temu 的 iframe 发送到 content-script 用来导出数据
  exportCorssOriginData = 'export-corss-origin-data',

  // temu 的 iframe 发送到 content-script 用来导入数据
  importCorssOriginData = 'import-corss-origin-data',

  // temu 页面中 content-script 发送到 iframe 来发送数据
  sendCorssOriginData = 'send-corss-origin-data',
}

export interface OpenTemuDialogMessage {
  type: PostMessageType.openTemuDialog;
}

export interface CloseTemuDialogMessage {
  type: PostMessageType.closeTemuDialog;
}

export interface SaveTemuSettingsMessage {
  type: PostMessageType.saveTemuSettings;
  data: {
    settings: string;
    suggestedName?: string;
  };
}

export interface ImportTemuSettingsMessage {
  type: PostMessageType.importTemuSettings;
}

export interface SendTemuSettingsMessage {
  type: PostMessageType.sendTemuSettings;
  data: {
    settings: string;
  };
}

export interface ExportCorssOriginDataMessage {
  type: PostMessageType.exportCorssOriginData;
  data: {
    data: string;
    suggestedName: string;
  };
}

export interface ImportCorssOriginDataMessage {
  type: PostMessageType.importCorssOriginData;
}

export interface SendCorssOriginDataMessage {
  type: PostMessageType.sendCorssOriginData;
  data: {
    data: string;
  };
}

export interface ChangeBackgroundMessage {
  data: {
    sourceImage: File;
    taskId: string;
  };
  type: PostMessageType.changeBackground;
}

export interface GetChangedBackgroundResultMessage {
  type: PostMessageType.getChangedBackgroundResult;
  data: {
    errorCode: 0 | 1;
    resultUrl: string;
    resultBlob: Blob;
    taskId: string;
    errorMessage?: string;
  };
}

export enum StateType {
  collect = 'collectState',
  process = 'processState',
  push = 'pushState',
}

export enum CollectState {
  init = '未采集',
  success = '采集成功',
  error = '采集失败',
}

export enum ProcessState {
  init = '未加工',
  success = '加工成功',
  error = '加工失败',
}

export enum PushState {
  init = '未发布',
  success = '发布成功',
  error = '发布失败',
}

export interface Config {
  gigaDelimiter: string;
  gigaAvailableSettingsTabValue: string;
  temuAvailableSettingsTabValue: string;
  alertDialogZIndex: number;
  dialogZIndex: number;
  sheetZIndex: number;
  SelectZindex: number;
  dropdownMenuZIndex: number;
  iframeZIndex: number;
  proxyDirectLabel: string;
  NotFoundBaseGigaProductPrice: number;
  temu: {
    temuAvaliableSynchronizationTableKey: string;
    temuAvaliableSynchronization: string;
    temuAvaliableSynchronizationMallTableData: string;
    temuRegidtrationActivityTableKey: string;
    temuRegidtrationActivityKey: string;
    temuRegidtrationActivityMallTableData: string;
  };
  dianxiaomi: {
    publicKey: string;
  };
}

export default {
  gigaDelimiter: '[gg]',
  gigaAvailableSettingsTabValue: 'giga-settings-tab-value',
  temuAvailableSettingsTabValue: 'temu-settings-tab-value',
  dialogZIndex: 10000,
  sheetZIndex: 10005,
  SelectZindex: 10010,
  dropdownMenuZIndex: 10020,
  alertDialogZIndex: 10030,
  iframeZIndex: 90000,
  proxyDirectLabel: '直连',

  // 当 giga 中的价钱无法获取时,设置为 -1000
  NotFoundBaseGigaProductPrice: -1000,
  temu: {
    // 主要存储库存同步时表格的分页等信息
    temuAvaliableSynchronizationTableKey: 'temu-avaliable-synchronization-table',

    // 主要存储库存同步时活动条件和选择的店铺站点销售状态等信息
    temuAvaliableSynchronization: 'temu-avaliable-synchronization',

    // 主要存储同步库存时的表格信息
    temuAvaliableSynchronizationMallTableData: 'temu-avaliable-synchronization-mall-table-data',

    // 主要存储报名活动时表格的分页等信息
    temuRegidtrationActivityTableKey: 'temu-regidtration-activity-table',

    // 主要存储报名活动时活动条件和选择的店铺站点销售状态等信息
    temuRegidtrationActivityKey: 'temu-regidtration-activity',

    // 主要存储上报活动时的表格信息
    temuRegidtrationActivityMallTableData: 'temu-regidtration-activity-mall-table-data',
  },

  dianxiaomi: {
    // 店小密发布时的持久化设置
    publicKey: 'diaoxiaomi-public-key',
  },
} satisfies Config;

// 省份类型,中国时使用
export interface Region2 {
  name: string;
  value: string;
}

export const mimeToExtMap: Record<string, string> = {
  'image/jpeg': '.jpg',
  'image/jpg': '.jpg',
  'image/png': '.png',
  'image/gif': '.gif',
  'image/webp': '.webp',
  'image/avif': '.avif',
  'image/svg+xml': '.svg',
  'application/pdf': '.pdf',
  // 可以继续扩展其他类型
};

export const extToMimeMap: Record<string, string> = {
  '.jpg': 'image/jpeg',
  '.png': 'image/png',
  '.gif': 'image/gif',
  '.webp': 'image/webp',
  '.avif': 'image/avif',
  '.svg': 'image/svg+xml',
  '.pdf': 'application/pdf',
  // 可以继续扩展其他类型
};
